<?php

namespace app\index\controller;

use tian\Session;

class Friends extends Base
{
    public function index()
    {
        try {
            $role_id = Session::get('uid');
            $modelFriends = model('Friends');
            $modelMsg = model('FriendsMsg');
            $modelRole = model('Role');

            $where = [];
            $where[] = ['sid', '=', $role_id];
            $list = $modelFriends->getList($where);
            if (!empty($list)) {
                foreach ($list as $k => $vo) {
                    $infoRole = $modelRole->getInfo($vo['uid']);
                    $vo['uid_lv'] = $infoRole['ulv'];
                    $vo['uid_state'] = state_hone($infoRole['ulv'])['name'];
                    $vo['uid_name'] = $infoRole['uname'];
                    if ($infoRole['sfzx'] == 1 && rtdate(timestamp($infoRole['endtime'])) === date('Y-m-d')) {
                        $vo['sfzx'] = 1;
                    } else {
                        $vo['sfzx'] = 0;
                    }

                    $vo['str'] = str_base64_encode($vo['id'], $role_id);

                    //验证是否有回复消息
                    $vo['msgStatus'] = 0; //默认没有消息
                    $mapMsg = [];
                    //$mapMsg[] = ['im_id', '=', $vo['id']];
                    //$mapMsg[] = ['uid', '=', $role_id];
                    $mapMsg[] = ['sid_uid', '=', $vo['uid'].'|'.$role_id];
                    $listMsg = $modelMsg->getList($mapMsg);
                    if (!$listMsg->isEmpty()) {
                        $vo['msgStatus'] = 1;
                    }
                }
            }
            $this->assign('list', $list);

            $this->fetch('index');
        } catch (\Throwable $e) {
            echo $e->getMessage();
        }
    }

    public function email()
    {
        try {
            $role_id = Session::get('uid');
            $modelMsg = model('FriendsMsg');
            $modelRole = model('Role');

            $str = input('str');
            $this->assign('str', $str);

            $id = $this->strAnalysis($str, $role_id);
            $info = model('Friends')->getInfo($id);

            $str1 = $info['sid'].'|'.$info['uid'];
            $str2 = $info['uid'].'|'.$info['sid'];
            $listMsg = $modelMsg->where('sid_uid', $str1)->whereOr('sid_uid', $str2)->order('id asc')->select();
            if (!empty($listMsg)) {
                foreach ($listMsg as $k => $item) {
                    $intersection = array_diff(explode('|',$item['sid_uid']), [$item['uid']]);

                    $infoRole = $modelRole->getInfo($intersection);
                    $item['uname'] = $infoRole['uname'];
                }
            }

            $this->assign('listMsg', $listMsg);

            $this->fetch('email');
        } catch (\Throwable $e) {
            echo $e->getMessage();
        }
    }

    public function emailAct()
    {
        try {
            $role_id = Session::get('uid');

            $msg = input('msg');

            if ($msg == '') {
                returnJson(0, '标题和内容不能为空');
            }

            $str = input('str');
            $id = $this->strAnalysis($str, $role_id);
            $info = model('Friends')->getInfo($id);

            $add = [];
            $add['msg'] = $msg;
            $add['uid'] = $info['uid'];
            $add['im_id'] = $id;
            $add['sid_uid'] = $role_id . '|' . $info['uid'];
            model('FriendsMsg')->add($add);

            returnJson(1, 'ok');
        } catch (\Throwable $e) {
            echo $e->getMessage();
        }
    }

    public function del()
    {
        try {
            $role_id = Session::get('uid');
            $str = input('str');

            $id = $this->strAnalysis($str, $role_id);
            $info = model('Friends')->getInfo($id);

            $str1 = $info['sid'].'|'.$info['uid'];
            $str2 = $info['uid'].'|'.$info['sid'];

            $map1['sid_uid'] = $str1;
            model('FriendsMsg')->where($map1)->delete();

            $map2['sid_uid'] = $str2;
            model('FriendsMsg')->where($map2)->delete();

            returnJson(1, 'ok');
        } catch (\Throwable $e) {
            echo $e->getMessage();
        }
    }

}
